﻿using System;
using System.Data;
using SQLPXBase.Factory.Keys;

namespace SQLPXBase.Factory
{
    public abstract class FactorizableModel<PX>
        where PX : Interfaces.ISQLProxyBase, new()
    {
        private ModelFactory<PX> _factory;
        public ModelFactory<PX> factory
        {
            get
            {
                if (_factory == null)
                    throw new UnknownFactoryException();
                return _factory;
            }
        }

        protected FactorizableModel(ModelFactory<PX> factory)
        {
            this._factory = factory;

            Utils.InitializeLazyKeysAndExtensionKeys(this, factory);
        }

        public abstract PrimaryKey GetPK();
        internal protected abstract void Load(PrimaryKey id);
        internal protected abstract void InitializeFromDB(DataRow rw);
    }

    public class UnknownFactoryException : Exception { }
}
